package com.beta.admin.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.beta.common.web.AbstractBaseController;
import com.beta.common.web.ContentType;
import com.beta.dto.Shipment.ShipmentsDto;
import com.beta.em.order.ShipmentStatus;
import com.beta.entity.Shipments;
import com.beta.admin.request.Shipment.ShipmentRequest;
import com.beta.service.IShipmentsService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * Created by lichao on 2017/1/9.
 */
@RestController
@RequestMapping(value = RequestMappingConstants.MAPPING_PREFIX + "/shipments")
public class ShipmentsController extends AbstractBaseController {
    @Autowired
    private IShipmentsService shipmentsService;
    @RequiresPermissions("shipments:list")
    @GetMapping(value = "",produces = ContentType.JSON)
    public Object getShipments(@RequestParam("code")String code,@RequestParam("orderId")Integer orderId,
                                @RequestParam("courierName")String courierName,@RequestParam("address")String address){
        Page<Shipments> shipments = shipmentsService.getShipments(getMybatisPage(), code, orderId, courierName, address);
        return pageData(shipments);
    }
    @RequiresPermissions("shipments:view")
    @GetMapping(value = "/{orderId}",produces = ContentType.JSON)
    public Object getShipmentsDetail(@PathVariable("orderId")Integer orderId){
        Shipments shipment = shipmentsService.getShipmentByOrderId(orderId);
        shipment.setStatus(ShipmentStatus.getCnValue(shipment.getStatus()));
        return ok(shipment);
    }
    @RequiresPermissions("shipments:edit")
    @PatchMapping(value = "/{orderId}")
    public Object updateShipmentReceiver(@PathVariable("orderId")Integer orderId,
                                         @Valid @RequestBody ShipmentRequest request){
        ShipmentsDto shipmentsDto=new ShipmentsDto();
        shipmentsDto.setReceiverName(request.getReceiverName());
        shipmentsDto.setReceiverPhone(request.getReceiverPhone());
        shipmentsDto.setReceiverAddress(request.getReceiverAddress());
        shipmentsService.updateShipmentsReceiver(orderId,shipmentsDto);
        return ok();
    }
}
